﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.ComponentModel;
using CommonTools;

namespace AssemblySigner
{
    public partial class KeySelectionCombo : UserControl
    {
        public class KeyFileItem
        {
            public string KeyName { get; set; }
            public string FilePath { get; set; }

            public KeyFileItem() { }
            public KeyFileItem(string name, string path)
            {
                KeyName = name; FilePath = path;
            }
        }

        private KeyFileItem _openKeyItem = new KeyFileItem() { KeyName = "<Browse...>" };
        private KeyFileItem _newKeyItem = new KeyFileItem() { KeyName = "<Create...>" };

        private AssemblyHelper _assemblyHelper = new AssemblyHelper();

        public KeySelectionCombo()
        {
            InitializeComponent();

            comboKeys.Items.Add(_openKeyItem);
            comboKeys.Items.Add(_newKeyItem);
        }

        private string _lastSelectedKey;
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedKeyFile
        {
            get { return _lastSelectedKey; }
            set { SetKeyFile(value); }
        }

        public event EventHandler SelectedIndexChanged;

        private void comboKeyFile_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboKeys.SelectedItem == _openKeyItem)
            {
                if (dlgOpenKey.ShowDialog(this.FindForm()) == DialogResult.OK)
                    SetKeyFile(dlgOpenKey.FileName);
                else
                    SetKeyFile(_lastSelectedKey);

            }
            else if (comboKeys.SelectedItem == _newKeyItem)
            {
                dlgCreateNewKey.FileName = "NewKey.snk";

                if (dlgCreateNewKey.ShowDialog(this.FindForm()) == DialogResult.OK)
                {
                    string newKeyFile = dlgCreateNewKey.FileName;

                    _assemblyHelper.CreateKeyPair(newKeyFile);
                    MessageBox.Show("Key was created", _assemblyHelper.Output, MessageBoxButtons.OK, MessageBoxIcon.Information);

                    SetKeyFile(newKeyFile);
                }
                else
                    SetKeyFile(_lastSelectedKey);
            }
            else if (comboKeys.SelectedItem != null)
            {
                SetKeyFile((comboKeys.SelectedItem as KeyFileItem).FilePath);
            }

            if (SelectedIndexChanged != null)
                SelectedIndexChanged(sender, e);
        }

        public KeyFileItem AddKeyFile(string keyFile)
        {
            if (string.IsNullOrEmpty(keyFile))
                return null;

            KeyFileItem existing = null;

            foreach (KeyFileItem item in comboKeys.Items)
                if (item.FilePath == keyFile)
                    existing = item;

            if (existing != null)
            {
                if (File.Exists(keyFile))
                    return existing;
                else
                {
                    comboKeys.Items.Remove(existing);
                    return null;
                }
            }
            else
            {
                var newKeyItem = new KeyFileItem(Path.GetFileName(keyFile), keyFile);
                comboKeys.Items.Insert(0, newKeyItem);

                return newKeyItem;
            }
        }

        public void SetKeyFile(string keyFile)
        {
            var keyItem = AddKeyFile(keyFile);

            if (keyItem != null)
            {
                comboKeys.SelectedItem = keyItem;
                _lastSelectedKey = keyFile;
            }
            else
                comboKeys.SelectedItem = null;
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string[] Keys
        {
            get
            {
                var keys = new List<string>();

                foreach (KeyFileItem key in comboKeys.Items)
                    if (!string.IsNullOrEmpty(key.FilePath))
                        keys.Add(key.FilePath);

                return keys.ToArray();
            }
            set
            {
                comboKeys.Items.Clear();
                comboKeys.Items.Add(_openKeyItem);
                comboKeys.Items.Add(_newKeyItem);

                foreach (string fileName in value)
                    AddKeyFile(fileName);
            }
        }

    }
}


